from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates


templates = Jinja2Templates(directory="templates")

app = FastAPI()


@app.get("/")
@app.get("/home")
async def main_page(request: Request):
    return template(request, "home.html")


@app.get("/list")
async def home_page(request: Request):
    items = ["路人甲", "路人乙", "路人丙"]
    title = "这是标题"

    return template(request, "list.html", title=title, items=items)


def template(request: Request, tmpl, **kwargs):
    kwargs["request"] = request
    return templates.TemplateResponse(tmpl, context=kwargs, media_type="text/html")
